package leetcode_core.leetcode_4;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2022年11月04日 8:05
 */
public class ReachNumber {
    public int reachNumber(int target) {
        Queue<int[]> q = new LinkedList<>();
        //begin[0]:startPoint begin[1]:下一步要走的步数
        int[] begin = new int[]{0,1};
        q.offer(begin);
        while (!q.isEmpty()){
            int[] head = q.poll();
            int start = head[0];
            int step =  head[1];
            if(start == target){
                return step-1;
            }
            q.offer(new int[]{start+step,step+1});
            q.offer(new int[]{start-step,step+1});
        }
        return -1;
    }
}
